import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 获取运行环境信息
const systemInfo = uni.getSystemInfoSync()
const equipment = systemInfo.platform //运行环境
const screenWidth = systemInfo.screenWidth //屏幕宽度px
const screenHeight = systemInfo.screenHeight //屏幕宽度px
const pixelRatio = 750 / systemInfo.screenWidth //rpx比例
let navbarHeight = '' //导航栏高度
let navbarPdt = '' //导航栏上内边距
let navbarWidth = '' //导航栏宽度
let navbarPdr = '0' //导航栏右边距
if (equipment == 'android') {
	navbarHeight = systemInfo.safeArea.top * pixelRatio + 48 * pixelRatio + 10
	navbarPdt = 48 * pixelRatio + 'rpx'
	navbarWidth = '750rpx'
} else if (equipment == 'ios') {
	navbarHeight = systemInfo.safeArea.top * pixelRatio + 44 * pixelRatio + 10
	navbarPdt = 44 * pixelRatio + 'rpx'
	navbarWidth = '750rpx'
} else if (uni.getMenuButtonBoundingClientRect() != '' && uni.getMenuButtonBoundingClientRect() != undefined) {
	const MenuButton = uni.getMenuButtonBoundingClientRect() //小程序获取右侧胶囊信息
	navbarHeight = MenuButton.top * pixelRatio + MenuButton.height * pixelRatio + 2 * pixelRatio + 10
	navbarPdt = MenuButton.top * pixelRatio + 'rpx'
	navbarPdr = 750 - MenuButton.left * pixelRatio + 'rpx'
	navbarWidth = '750rpx'
}



let isLogin = uni.getStorageSync('token')
let userInfo = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {}
let musicId = uni.getStorageSync('musicId') ? uni.getStorageSync('musicId') : ''
let musicUrl = uni.getStorageSync('musicUrl') ? uni.getStorageSync('musicUrl') : 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3'
let musicDetail = uni.getStorageSync('musicDetail') ? uni.getStorageSync('musicDetail') : {}
let playList = uni.getStorageSync('playList') ? uni.getStorageSync('playList') : []
let playMode = uni.getStorageSync('playMode') ? uni.getStorageSync('playMode') : 'loop'
let playTime = uni.getStorageSync('playTime') ? uni.getStorageSync('playTime') : 0
let musicLong = uni.getStorageSync('musicLong') ? uni.getStorageSync('musicLong') : 0
let lyric = uni.getStorageSync('lyric') ? uni.getStorageSync('lyric') : []

const store = new Vuex.Store({
	state: {
		//自定义导航栏组件信息
		navbarMessage: {
			width: navbarWidth,
			height: navbarHeight + 'rpx',
			paddingTop: navbarPdt,
			paddingRight: navbarPdr
		},
		screenWidth: screenWidth, //屏幕宽度px
		pixelRatio: pixelRatio, //rpx比例
		screenHeight: screenHeight * pixelRatio, //屏幕高度rpx
		navbarHeight: navbarHeight, //状态栏高度rpx
		isLogin: false, //账号是否登录
		userInfo: userInfo,	//用户信息
		isPlayingMusic: false,	//是否正在播放音乐
		currentMusicId: musicId,	//当前播放音乐的Id
		currentMusicUrl: musicUrl,	// 当前音乐的播放链接
		currentPlayTime: playTime,	//当前音乐的播放时间
		currentMusicLong: musicLong,	//当前音乐的总时长
		lyric: lyric,	//当前歌曲歌词
		currentMusicDetail: musicDetail	,//当前歌曲详情
		currentPlayList: playList,	//当前播放列表
		playMode: playMode	,//当前播放模式
		defaultSearch: {}	,//当前的默认搜索关键字
		videoList: []	//视频列表
	},
	mutations: {
		login(state, userInfo) { //登录账号
			state.isLogin = true
			state.userInfo = userInfo
			uni.setStorageSync('userInfo',userInfo)
		},
		pdLoginStatus(state) { //判断登录状态
			state.isLogin = uni.getStorageSync('token') ? true : false
		},
		changeCurrentMusicId(state,id) {	//修改当前播放音乐播放的id
			state.currentMusicId = id
			uni.setStorageSync('musicId', id)
		},
		changeCurrentPlayTime(state,time) {		//修改当前播放音乐的播放时间
			state.currentPlayTime = time
			uni.setStorageSync('playTime', time)
		},
		changeCurrentMusicLong(state,long) {	//修改当前音乐总时长
			state.currentMusicLong = long
			uni.setStorageSync('musicLong', long)
		},
		changeLyric(state,lyric) {	//修改当前歌词
			state.lyric = lyric
			uni.setStorageSync('lyric', lyric)
		},
		changeCurrentMusicDetail(state,detail) {	//修改当前播放音乐播放的详细信息
			state.currentMusicDetail = detail
			uni.setStorageSync('musicDetail', detail)
		},
		changeCurrentMusicUrl(state,url) {	//修改当前播放音乐播放的地址
			state.currentMusicUrl = url
			uni.setStorageSync('musicUrl', url)
		},
		changeMusicPlayStatus(state, status) { //修改音乐播放状态
			if(status != undefined) {
				state.isPlayingMusic = status
			} else {
				state.isPlayingMusic = !state.isPlayingMusic
			}
		},
		changePlayMode(state, mode) {	//切换歌曲播放模式
			state.playMode = mode
			uni.setStorageSync('playMode', mode)
		},
		changePlayList(state, playList) {	//切换播放歌曲列表
			state.currentPlayList = playList
			uni.setStorageSync('playList', playList)
		},
		changeDefaultSearch(state,keyWord) {	//修改当前的默认搜索关键字
			state.defaultSearch = keyWord
		},
		changeVideoList(state,videoList) {
			state.videoList = videoList
		}
	},
	getters: {}
})

export default store
